
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.SYS;
using Apps.Models.SYS;
using Apps.IBLL.SYS;
namespace Apps.BLL.SYS
{
	public partial class SYS_ROLEUSERBLL: Virtual_SYS_ROLEUSERBLL,ISYS_ROLEUSERBLL
	{
        

	}
	public class Virtual_SYS_ROLEUSERBLL
	{
        [Dependency]
        public ISYS_ROLEUSERRepository m_Rep { get; set; }

		public virtual List<SYS_ROLEUSERModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<SYS_ROLEUSER> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.ROLE_ID.Contains(queryStr)
								|| a.USER_ID.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<SYS_ROLEUSERModel> GetPagerList(ref GridPager pager, Expression<Func<SYS_ROLEUSER, bool>> whereLambda )
        {

            IQueryable<SYS_ROLEUSER> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<SYS_ROLEUSERModel> GetList(Expression<Func<SYS_ROLEUSER, bool>> whereLambda )
        {

            IQueryable<SYS_ROLEUSER> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<SYS_ROLEUSERModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<SYS_ROLEUSERModel>();
		}

        public virtual List<SYS_ROLEUSERModel> CreateModelList(ref IQueryable<SYS_ROLEUSER> queryData)
        {

            List<SYS_ROLEUSERModel> modelList = (from r in queryData
                                              select new SYS_ROLEUSERModel
                                              {
													ROLE_ID = r.ROLE_ID,
													USER_ID = r.USER_ID,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, SYS_ROLEUSERModel model)
        {
            try
            {
                SYS_ROLEUSER entity = m_Rep.GetById(model.ROLE_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new SYS_ROLEUSER();
               				entity.ROLE_ID = model.ROLE_ID;
				entity.USER_ID = model.USER_ID;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<SYS_ROLEUSER, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.SYS_ROLEUSER.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, SYS_ROLEUSERModel model)
        {
            try
            {
                SYS_ROLEUSER entity = m_Rep.GetById(model.ROLE_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.ROLE_ID = model.ROLE_ID;
				entity.USER_ID = model.USER_ID;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual SYS_ROLEUSERModel GetById(object id)
        {
            if (IsExists(id))
            {
                SYS_ROLEUSER entity = m_Rep.GetById(id);
                SYS_ROLEUSERModel model = new SYS_ROLEUSERModel();
                              				model.ROLE_ID = entity.ROLE_ID;
				model.USER_ID = entity.USER_ID;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual SYS_ROLEUSERModel GetItem(Expression<Func<SYS_ROLEUSER, bool>> whereLambda)
        {
 
                SYS_ROLEUSER entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                SYS_ROLEUSERModel model = new SYS_ROLEUSERModel();
                              				model.ROLE_ID = entity.ROLE_ID;
				model.USER_ID = entity.USER_ID;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<SYS_ROLEUSERModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<SYS_ROLEUSERModel>(x => x.ROLE_ID, "ROLE_ID");
				 excelFile.AddMapping<SYS_ROLEUSERModel>(x => x.USER_ID, "USER_ID");
 
            //SheetName
            var excelContent = excelFile.Worksheet<SYS_ROLEUSERModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new SYS_ROLEUSERModel();
						 				  entity.ROLE_ID = row.ROLE_ID;
				  entity.USER_ID = row.USER_ID;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<SYS_ROLEUSERModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        SYS_ROLEUSER entity = new SYS_ROLEUSER();
                       						entity.ROLE_ID = model.ROLE_ID;
						entity.USER_ID = model.USER_ID;
 
                        db.SYS_ROLEUSER.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
